package com.gexin.model.notice;

import com.gexin.common.utils.SQLUtils;
import com.gexin.model.notice.base.BaseNoticeNewsComment;
import com.gexin.model.weixin.WeixinUser;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * 业务名称:  t_notice_news_comment(资讯评论表) 
 */
@SuppressWarnings("serial")
public class NoticeNewsComment extends BaseNoticeNewsComment<NoticeNewsComment> {
	public static final NoticeNewsComment dao = new NoticeNewsComment();

	public Page<NoticeNewsComment> query(int pageNumber,int pageSize,NoticeNewsComment comment){
		List<Object> list =new LinkedList<Object>();
		SQLUtils sql=new SQLUtils(" from t_notice_news_comment c  left join t_notice_news n on n.id =c.news_id left join t_weixin_user u on u.id=c.user_id  where 1=1 ");
		if(comment!=null){
			if(!StringUtils.isEmpty(comment.getContent())){
				sql.append(" and c.content like ? ");
				list.add("%"+comment.getContent()+"%");
			}
			if(comment.getState()!=null){
				sql.append(" and c.state = ? ");
				list.add(comment.getState());
			}
			if(comment.getNewsId()!=null){
				sql.append(" and c.news_id = ? ");
				list.add(comment.getNewsId());
			}
		}
		sql.append("   order by  c.create_time desc  ");
		return paginate(pageNumber,pageSize,"select c.*,n.title,u.username,u.nickname",sql.toString(),list.toArray());
	}

	public List<NoticeNewsComment> findByNoticeId(Integer noticeId) {
		return	find("select nnc.*,u.headimgurl,u.username,u.nickname from t_notice_news_comment nnc left join t_weixin_user u on u.id=nnc.user_id  where nnc.news_id=?",noticeId);
	}

	public List<NoticeNewsComment> findByUserId(WeixinUser authUser) {
		return find("select comm.*,notice.title from t_notice_news_comment comm left join t_notice_news notice on comm.news_id=notice.id where comm.user_id=?",authUser.getId());
	}
}
